<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

App::import('Vendor', 'ClassEzpdf', array('file' => 'pdf/class.ezpdf.php'));
$pdf = new Cezpdf('LETTER');
$pdf->ezStartPageNumbers(20, 75, 10, 'right', utf8_decode('Página' . "  {PAGENUM} de {TOTALPAGENUM}"), 1);
generar_carta(' ', "", $pdf);

foreach ($proponente as $prop) {
    $str = "Señor(a):";
    $pdf->ezText(utf8_decode($str), 12, array('justification' => 'left'));
    $pdf->ezText(utf8_decode($prop['Proposer']['nombre'] . ":\n$codigo\nConvocatoria INCODER SIT-01-2011\n" . $prop['Proposer']['direccion'] . "\n" . $prop['City']['name']), 12, array('justification' => 'left'));
}


//Cargamos requisitos del proyecto
///IMPRESION REQUISITOS PREDIOS
$pdf->ezText("\n", 12, array('justification' => 'left'));

App::Import('model', 'Property');
$prdeq = new Property();
$prdeq->recursive = 0;
$predios = $prdeq->find('all', array('conditions' => array('Property.proyect_id' => $pid), 'fields' => array('Property.id', 'Property.nombre', 'Property.codigo', 'City.ley_segunda')));


foreach ($predios as $predio) {
    $data = array();
    $calif = "";
    $obs = "";
    $prdeq->PropertyRequirement->recursive = 0;
    $data[] = array('Requisito' => utf8_decode("<b> Nombre del predio:</b>"), 'Observaciones' => utf8_decode("<b>" . $predio['Property']['nombre'] . "</b>"));
    //predios en ley segunda
 
    //$prdeq->TitleStudy->recursive = -1;
    $titulo = $prdeq->TitleStudy->find('first', array('conditions' => array('TitleStudy.property_id' => $predio['Property']['id']), 'order' => array('TitleStudy.id DESC'), 'fields' => array('TitleStudy.id', 'TitleStudy.concepto', 'TitleStudy.calificacion', 'TitleStudy.hipoteca', 'TitleStudy.valor_hipoteca', 'TitleStudy.recomendaciones', 'TitleStudy.area_obs', 'TitleStudy.linderos')));
    $prdeq->TitleStudy->RequirementsTitleStudy->recursive=2;
    $reqts=$prdeq->TitleStudy->RequirementsTitleStudy->find('all',array('conditions'=>array('RequirementsTitleStudy.title_study_id'=>$titulo['TitleStudy']['id']),'fields'=>array('RequirementsTitleStudy.concepto','RequirementsTitleStudy.requirement_id') ));
    App::Import('model','Requirement');
    foreach ($reqts as $reqt) {
        $nomr=new Requirement();
        $txr=  $nomr->field('texto',array('Requirement.id'=>$reqt['RequirementsTitleStudy']['requirement_id']));
    
         $data[] = array('Requisito' => utf8_decode($txr), 'Observaciones' => utf8_decode($reqt['RequirementsTitleStudy']['concepto']));
   
    }
    
    $calE = "";
    switch ($titulo['TitleStudy']['calificacion']) {
        case 0:
            $calE = "No Cumple";


            break;
        case 1:
            $calE = "Cumple";

            break;
        case 2:
            $calE = "No cumple";

            break;
        case 3:
            $calE = "No cumple";

            break;



        default:
            break;
    }
    if ($titulo['TitleStudy']['hipoteca'] == 1)
    $data[] = array('Requisito' => utf8_decode("VALOR HIPOTECA"), 'Observaciones' => utf8_decode($titulo['TitleStudy']['valor_hipoteca']));
    $data[] = array('Requisito' => utf8_decode("LINDEROS"), 'Observaciones' => utf8_decode($titulo['TitleStudy']['linderos']));
    $data[] = array('Requisito' => utf8_decode("OBSERVACIONES AREA"), 'Observaciones' => utf8_decode($titulo['TitleStudy']['area_obs']));
    $data[] = array('Requisito' => utf8_decode("RECOMENDACIONES"), 'Observaciones' => utf8_decode($titulo['TitleStudy']['recomendaciones']));
    $data[] = array('Requisito' => utf8_decode("CONCEPTO"), 'Observaciones' => utf8_decode($titulo['TitleStudy']['concepto']));
    $data[] = array('Requisito' => utf8_decode("CALIFICACIÓN"), 'Observaciones' => utf8_decode($calE));

    $pdf->ezTable(
            $data, null, " Predio ".$predio['Property']['codigo'], array(
        'fontSize' => 7,
        'titleFontSize' => 12,
        'width' => 500,
        'showHeadings' => 0,
        'showLines' => 2,
        'shaded' => 0,
        'protectRows' =>3,
        'cols' => array('Requisito' => array('justification' => 'full', 'width' => 150), 'Observaciones' => array('justification' => 'full', 'width' => 350)),
            )
    );
    $pdf->ezText("\n", 12, array('justification' => 'left'));

}


$pdf->ezStopPageNumbers(1, 1, 0);
$pdf->ezStream();
?>

<?php

function generar_carta($cod, $str, &$pdf, $start=true, $m_left=2.5, $m_right=2.5) {
    App::import('Vendor', 'ClassEzpdf', array('file' => 'pdf/class.ezpdf.php'));
    if ($pdf == NULL) {
        $pdf = new Cezpdf('LETTER');
    }
    if ($start) {
        $tmp = array(
            'b' => 'Trebuchet-Bold.afm'
            , 'i' => 'Trebuchet-Italic.afm'
            , 'bi' => 'Trebuchet-BoldItalic.afm'
            , 'ib' => 'Trebuchet-BoldItalic.afm'
            , 'bb' => 'Trebuchet-Bold.afm'
        );
        $pdf->setFontFamily('Trebuchet.afm', $tmp);
        $pdf->selectFont('fonts/Trebuchet.afm');

        //$pdf->ezSetCmMargins(5.5,5.5,4,4);

        $all = $pdf->openObject();
        $pdf->saveState();
        $pdf->addPngFromFile('img/cabecera.png', 60, 670, 500);
        $pdf->addPngFromFile('img/pie_de_pagina1.png', 100, 60, 550, 60);
        $pdf->addPngFromFile('img/pie_de_pagina.png', 0, 0, 610, 60);
        $pdf->restoreState();
        $pdf->closeObject();
        $pdf->addObject($all, 'all');
        $pdf->transaction('start');
    }

    $pdf->ezSetCmMargins(5, 5, $m_left, $m_right);

    $textOptions = array('justification' => 'full');
    $data = explode("\n", $str);
    $collecting = 0;
    foreach ($data as $d) {
        chop($d);
        if (strlen($d) && $d[0] == '#') {
            // comment, or new page request
            switch ($d) {
                case '#NP':
                    $pdf->ezNewPage();
                    break;
            }
        } else {
            $pdf->ezText($d, 8, $textOptions);
        }
    }
}
?>





